package com.woniuxy.qiantai.listener;

import com.google.code.kaptcha.Producer;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import java.io.IOException;
//用户下单给商家邮箱发送订单信息
@Component
public class OrderEmailListener {

    @Autowired
    Producer producer;

    @Autowired
    JavaMailSender javaMailSender;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @RabbitListener(queues = "OrderEmailQueue")
    //邮箱发送订单ID
    public void OrderEmail(Integer orderId, Channel channel, Message message) throws IOException {
        System.out.println("向邮箱发送订单号= "+orderId);

        //发送订单邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("1004758983@qq.com");
        mailMessage.setTo("1004758983@qq.com");
        mailMessage.setSubject("有新订单请尽快处理");
        //String code = producer.createText();
        mailMessage.setText("新用户下单号 "+orderId);
        javaMailSender.send(mailMessage);

        //redis中缓存验证码
       // stringRedisTemplate.opsForValue().set(email,code,5,TimeUnit.MINUTES);

        //手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        System.out.println("已发送订单号");
    }

}
